package com.zhongyu.controller;

import com.zhongyu.config.MD5Utils;
import com.zhongyu.pojo.Cangku;
import com.zhongyu.pojo.Goods;
import com.zhongyu.pojo.User;
import com.zhongyu.service.GoodsService;
import com.zhongyu.vo.GoodsVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
/*
商品
 */
@RestController
@RequestMapping("/jibenxinxi/sp")
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, GoodsVo goodsVo){
        PageResult pageResult=goodsService.searchAll(pageNo,pageSize,goodsVo);
        return ResultVo.success(pageResult);

    }
    @GetMapping("/list")
    public ResultVo list(HttpSession session) {
        List<Goods> list = goodsService.searchAll();
        return ResultVo.success(list);

    }
    @GetMapping("/list1")
    public ResultVo list1(String name) {
        List<Goods> list = goodsService.searchByName(name);
        return ResultVo.success(list);
    }
    @GetMapping("/list3")
    public ResultVo list3(Integer id) {
        Goods list = goodsService.searchById(id);
        return ResultVo.success(list);
    }
    @GetMapping("/list2")
    public ResultVo list2(Integer providerId) {
        List<Goods> list = goodsService.searchId(providerId);
        return ResultVo.success(list);

    }
    @PostMapping("/update")
    public ResultVo update(@RequestBody Goods goods, HttpSession session) {
        try {
            goods.setSellingPrice(Double.valueOf(Double.toString(goods.getSellingPrice())));
            goods.setPurchasePrice(Double.valueOf(Double.toString(goods.getPurchasePrice())));
            goods.setModifyDate(LocalDateTime.now());
            goodsService.update(goods);
            return ResultVo.success("修改商品成功!");
        }catch (Exception ex){
            return ResultVo.failure("修改商品失败!",ex);
        }
    }

    @GetMapping("/del")
    public ResultVo del(Integer id) {
        try {
            goodsService.del(id);
            return ResultVo.success("删除商品!");
        } catch (Exception ex) {

            return ResultVo.failure("删除商品失败！", ex);
        }
    }
    @PostMapping("/add")
    public ResultVo add(@RequestBody Goods goods, HttpSession session){
        try {
            if (goodsService.searchByCode(goods.getCode()).size()>0){
                return ResultVo.failure("该商品已存在!");
            }
            goodsService.add(goods);
            return ResultVo.success("添加商品成功!");
        }catch (Exception ex){
            return ResultVo.failure("添加商品失败!",ex);
        }
    }


}
